<?php
class User_model extends CI_Model
{
    private $ci,$redis;
    public function __construct(){
        $this->load->database();
        $this->ci = & get_instance();
        //链接redis
        $this->ci->load->driver('cache');
        $this->ci->cache->redis->is_supported();
        $this->redis = $this->ci->cache->redis;
    }

    public function addUser($data){
    	$rs = $this->db->insert("user_info",$data);
        if($rs){
            return $this->db->insert_id();
        }else{
            return false;
        }
    }

    public function getUserByPhone($phone){
        $query = $this->db->select()->from("user_info")->where('phone', $phone)->limit(1)->get();
        if ($query->row()) {
            return $query->row_array();
        } else {
            return false;
        }
    }

	public function getUserByUid($uid){
        $query = $this->db->select()->from("user_info")->where('uid', $uid)->limit(1)->get();
        if ($query->row()) {
            return $query->row_array();
        } else {
            return false;
        }
    }

    public function getAllUser($data,$limit='all',$orderby=''){
        $this->db->select()->from("user_info")->where($data);
        if($limit!='all'){
             $limit = abs($limit);
             $this->db->limit($limit);
        }
        if($orderby!=''){
            $this->db->order_by($orderby);
        }
        $query =  $this->db->get();
        if($limit==1) return $query->row_array();
        return $query->result_array();
    }

	public function getUserPhone($start_phone,$end_phone){
		$this->db->where('phone >=', $start_phone);
		$this->db->where('phone <=', $end_phone);
		//$this->db->order_by("RAND()");
        $query = $this->db->select()->from("user_info")->get();
        if ($query->result()) {
            return $query->result_array();
        } else {
            return false;
        }
    }

    public function getUserInfoByMulti($array,$cache=false){
         $query = $this->db->select()->from("user_info")->where($array)->limit(1)->get();
        if ($query->row()) {
            $userData = $query->row_array();
            if($cache){
                unset($userData['password']);
                $uid = $userData['uid'];
                $this->redis->getClient()->hmset("USERINFO|$uid",$userData);
            }
            return $userData;
        } else {
            return false;
        }
    }

    public function updateUser($array,$where){
        $this->db->where($where);
        $rs = $this->db->update('user_info', $array);
        return $rs;
    }

    public function getUserInfo($array,$cache=false){
        if(!isset($array['uid'])) return false;
        $uid = $array['uid'];
        if($cache){//读取缓存  
            $userData =  $this->redis->getClient()->hgetall("USERINFO|$uid");
            if(!empty($userData)) return $userData;
        }
        $query = $this->db->select()->from("user_info")->where($array)->limit(1)->get();
        if ($query->row()) {
            $userData = $query->row_array();
            unset($userData['password']);
            $this->redis->getClient()->hmset("USERINFO|$uid",$userData);
            return $userData;
        } else {
            return false;
        }
    }

    public function addRegLog($data){
        $table = splitTable('log_reg', $data['uid']);
        $log_conn = $this->load->database('log',true);
        $rs = $log_conn->insert($table,$data);
        if($rs){
            return $log_conn->insert_id();
        }else{
            return false;
        }
    }

    public function addLoginLog($data){
        $table = splitTable('log_login', $data['uid']);
        $log_conn = $this->load->database('log',true);
        $rs = $log_conn->insert($table,$data);
        if($rs){
            return $log_conn->insert_id();
        }else{
            return false;
        }
    }

    public function getObjectLog($object,$uid,$limit='all'){
        $table = splitTable('log_'.$object, $uid);
        $log_conn = $this->load->database('log',true);
        $data = array('uid'=>$uid);
        $log_conn->select()->from($table)->where($data)->order_by('id','DESC');
        if($limit!='all'){
             $limit = abs($limit);
             $log_conn->limit($limit);
        }
        $query = $log_conn->get();
        return $query->result_array();
    }

}